<?php
class Duyethosotuyendung extends MY_Controller {
 
    /**
    * Responsable for auto load the model
    * @return void
    */
    public function __construct(){
		parent::__construct();
		$this->load->model('tuyendung_m','tuyendung');
		if(!$this->session->userdata('admin'))
			redirect(base_url('admin/login'));
    }
 
    /**
    * Load the main view with all the current model model's data.
    * @return void
    */
   public function index(){      
		$data = array();		
		//pagination settings
		$config['per_page'] = 10;
		$config['base_url'] = base_url('admin/duyethosotuyendung');
		$config['use_page_numbers'] = TRUE;
		$config['num_links'] = $this->config->item('num_links');
		$config['full_tag_open'] = '<ul>';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><a>';
		$config['cur_tag_close'] = '</a></li>';		
		$paged = $this->uri->segment(3);
		$paged = ($paged * $config['per_page']) - $config['per_page'];
		if ($paged < 0)
			$paged = 0;
		//filter post data
		$post = $this->input->post();
		if($post){
			$this->form_validation->set_rules('ten', 'ten', 'trim');
			$this->form_validation->set_rules('email', 'email', 'trim');
			$this->form_validation->set_rules('trang_thai', 'trang_thai', 'trim');
			$this->form_validation->run();
			$filter['ten']	=	$this->input->post('ten');
			$filter['email']	=	$this->input->post('email');
			$filter['trang_thai']	=	$this->input->post('trang_thai');
			$this->session->set_userdata('admin_filter', $filter);
		}else if($paged == 0)
			$this->session->unset_userdata('admin_filter');
		$tmp = array(
			'limit'		=>	$config['per_page'],
			'paged'		=>	$paged
		);
		//filter
		$filter = $this->session->userdata('admin_filter');
		if(!$filter) $filter = array();
		$filter = array_merge($filter, $tmp);
		//
		$config['total_rows'] = $this->tuyendung->get_hoso($filter, true);
		// echo $this->db->last_query();die;
		$data['hoso']		= $this->tuyendung->get_hoso($filter);
		$page = $this->uri->segment(3);
		$page = $page > 1 ? $page - 1 : 0;
		$data['stt']		= $page * $config['per_page'] + 1;
      $this->pagination->initialize($config);  
		//load the view
      $data['main_content'] = 'admin/duyethoso/nhatuyendung';
      $this->load->view('admin/includes/template', $data);
   }
	
	function nhatuyendung($id = false){
		$data = array();
		$id = $this->uri->segment(4);
		if($id){
			$data['nhatuyendung']	=	$this->tuyendung->get_object($id);
			if(!$data['nhatuyendung'])
				redirect('admin');
		}else{
			redirect('admin');
		}
		$data['main_content'] = 'admin/duyethoso/xemhsnhatuyendung';
      $this->load->view('admin/includes/template', $data);
	}
	
	function update(){
		$data = array();
		$hoso = $this->input->post('hoso');
		$status = $this->input->post('status');
		#echo '<pre>';print_r($post);die;
		if($hoso){
			$tmp = array();			
			foreach($hoso as $value)
				$tmp[]	=	array('id'=>$value,'trang_thai'=>$status,'ngay_duyet'=>date('Y-m-d H:i:s'));
			$updated = $this->tuyendung->cap_nhat_trang_thai($tmp);
			$this->session->set_flashdata('updated', 'Cập nhật trạng thái thành công');
		}else
			$this->session->set_flashdata('danger', 'Cập nhật trạng thái không thành công');
		//load the view
      redirect(base_url('admin/duyethosotuyendung'));
	}	
	
	function duyet(){
		$data = array();
		$id = $this->uri->segment(4);
		if($id){
			$tuyendung	=	$this->tuyendung->get_object($id);
			$tmp	= array();
			$tmp['id']	= $id;
			$tmp['trang_thai']	= 1;
			$tmp['ngay_duyet']	= date('Y-m-d H:i:s');
			$this->tuyendung->cap_nhat_trang_thai(array($tmp), true);			
			$tmp = array();
			$tmp['trang_thai']		= 0;
			$tmp['ngay_cap_nhat']	= $tuyendung->ngay_cap_nhat;			
			$tuyendung		= $this->tuyendung->get_hoso($tmp);
			if($tuyendung)
				$id = $tuyendung->id;
			else
				$id	= false;
			// echo $this->db->last_query();die;
		}else
			redirect(base_url('admin/duyethosotuyendung'));
		if($id)
			redirect(base_url('admin/duyethosotuyendung/nhatuyendung/'.$id));
		else	
			redirect(base_url('admin/duyethosotuyendung'));
	}
	
	function tocanhan(){
		$data = array();
		$id = $this->uri->segment(4);
		if($id){
			$tuyendung	=	$this->tuyendung->get_object($id);
			$user_id = $tuyendung->user_id;
			//update user to canhan
			$tmp	= array();
			$tmp['trang_thai']	= 1;
			$tmp['loai']	= 1;
			$this->load->model('users_m', 'users');
			$this->users->update_loai($tmp, $user_id);			
			//create new default canhan
			$tmp = array();
			$tmp['user_id']	=	$user_id;
			$tmp['ho_ten']		=	$tuyendung->ten;
			$tmp['ngay_sinh']	=	'1990-01-01';
			$tmp['gioi_tinh']	=	0;
			$tmp['hon_nhan']	=	0;
			$tmp['tinh_thanh_id']	=	1;
			$tmp['quan_huyen_id']	=	1;
			$tmp['di_dong1']	=	'0123456789';
			$tmp['can_nang']	=	50;
			$tmp['chieu_cao_m']	=	1;
			$tmp['chieu_cao_cm']	=	65;
			$tmp['vong1']	=	85;
			$tmp['vong2']	=	60;
			$tmp['vong3']	=	90;
			$tmp['avatar']	=	$tuyendung->avatar;
			$tmp['ngay_cap_nhat']	=	date('Y-m-d H:i:s');
			$id = $this->users->create_canhan($tmp);
			//xoa nhatuyendung
			if($id)
				$this->tuyendung->remove($tuyendung->id);
			//get next
			$tmp = array();
			$tmp['trang_thai']		= 0;
			$tmp['ngay_cap_nhat']	= $tuyendung->ngay_cap_nhat;			
			$tuyendung		= $this->tuyendung->get_hoso($tmp);
			if($tuyendung)
				$id = $tuyendung->id;
			else
				$id	= false;
			// echo $this->db->last_query();die;
		}else
			redirect(base_url('admin/duyethosotuyendung'));
		if($id)
			redirect(base_url('admin/duyethosotuyendung/nhatuyendung/'.$id));
		else	
			redirect(base_url('admin/duyethosotuyendung'));
	}
}